package com.jumi.microservice.state.impl;

import com.jumi.microservice.domain.GoodsDTO;
import com.jumi.microservice.state.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 商品状态管理器实现
 *
 * @author Administrator
 */
@Component
public class GoodsStateManagerImpl implements GoodsStateManager {

    /**
     * 商品状态工厂
     */
    @Autowired
    private GoodsStateFactory goodsStateFactory;
    /**
     * 已上架状态
     */
    @Autowired
    private PuttedOnShelvesGoodsState puttedOnShelvesGoodsState;

    /**
     * 已下架状态
     */
    @Autowired
    private PulledOffShelvesGoodsState pulledOffShelvesGoodsState;


    /**
     * 定时上架状态
     */
    @Autowired
    private TimingPuttedOnShelvesGoodsState timingPuttedOnShelvesGoodsState;

    /**
     * 创建一个商品
     *
     * @param goods
     * @throws Exception
     */
    @Override
    public void create(GoodsDTO goods) {
        puttedOnShelvesGoodsState.doTransition(goods);
    }

    /**
     * 当前这个商品能否执行编辑操作
     *
     * @param goods 商品
     * @return 能否执行编辑操作
     * @throws Exception
     */
    @Override
    public Boolean canEdit(GoodsDTO goods) {
        GoodsState state = goodsStateFactory.get(goods);
        return state.canEdit(goods);
    }

    /**
     * 已上架状态执行编辑商品的操作
     *
     * @param goods 商品
     * @throws Exception
     */
    @Override
    public void putOnShelvesEdit(GoodsDTO goods) {
        puttedOnShelvesGoodsState.doTransition(goods);
    }

    /**
     * 已下架状态执行编辑商品的操作
     *
     * @param goods 商品
     * @throws Exception
     */
    @Override
    public void pullOffShelvesEdit(GoodsDTO goods) {
        pulledOffShelvesGoodsState.doTransition(goods);
    }

    /**
     * 判断能否执行上架操作
     *
     * @param goods 商品
     * @return 能否执行上架操作
     * @throws Exception
     */
    @Override
    public Boolean canPutOnShelves(GoodsDTO goods) {
        GoodsState state = goodsStateFactory.get(goods);
        return state.canPutOnShelves(goods);
    }

    /**
     * 执行上架操作
     *
     * @param goods
     * @throws Exception
     */
    @Override
    public void putOnShelves(GoodsDTO goods) {
        puttedOnShelvesGoodsState.doTransition(goods);
    }

    /**
     * 判断能否执行下架操作
     *
     * @param goods 商品
     * @return
     * @throws Exception
     */
    @Override
    public Boolean canPullOffShelves(GoodsDTO goods) {
        GoodsState state = goodsStateFactory.get(goods);
        return state.canPullOffShelves(goods);
    }

    /**
     * 执行下架操作
     *
     * @param goods 商品
     * @throws Exception
     */
    @Override
    public void pullOffShelves(GoodsDTO goods) {
        pulledOffShelvesGoodsState.doTransition(goods);
    }

    /**
     * 能否执行删除操作
     *
     * @param goods 商品
     * @return
     * @throws Exception
     */
    @Override
    public Boolean canRemove(GoodsDTO goods) {
        GoodsState state = goodsStateFactory.get(goods);
        return state.canRemove(goods);
    }

}
